package com.zhenquan.doubanread.controller.portal;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zhenquan.doubanread.common.Const;
import com.zhenquan.doubanread.common.ResponseCode;
import com.zhenquan.doubanread.common.ServerResponse;
import com.zhenquan.doubanread.pojo.HaveRead;
import com.zhenquan.doubanread.pojo.MatchBean;
import com.zhenquan.doubanread.pojo.User;
import com.zhenquan.doubanread.pojo.WantRead;
import com.zhenquan.doubanread.service.IHaveReadService;
import com.zhenquan.doubanread.service.IWantReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/2/3 0003.
 */
@Controller
@RequestMapping("/wantread/")
public class WantReadController {
    @Autowired
    private IWantReadService iWantReadService;
    @Autowired
    private IHaveReadService iHaveReadService;

    @RequestMapping(value = "add.do" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> add(HttpSession session, Integer id, String title, String rating, String author, String pubdate, String origin_title, String image, String binding, String translator,
                                      String catelog, Integer pages, String image_large, String publisher, String isbn10, String isbn13, String author_intro, String summary, Double price,
                                      Integer categoryid) {
        User user = (User) session.getAttribute(Const.CURENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (id == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        WantRead wantRead = new WantRead(id, title, rating, author, pubdate, origin_title, image, binding, translator, catelog, pages, image_large, publisher, isbn10, isbn13, author_intro, summary, price, categoryid, user.getId());
        return iWantReadService.addOrRemove(wantRead);
    }

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(HttpSession session, int pageNum, int pageSize) {
        User user = (User) session.getAttribute(Const.CURENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iWantReadService.list(user.getId(), pageNum, pageSize);
    }

    @RequestMapping("matchlist.do")
    @ResponseBody
    public ServerResponse<List<MatchBean>> matchlist(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //1.获取想读的列表
        ServerResponse<List<WantRead>> list = iWantReadService.list(user.getId());
        //2、在读过表中查询userid不等于自己的，符合想读列表项的列表
        ServerResponse<List<HaveRead>> allList = iHaveReadService.selectHaveReadByNotMySelf(user.getId());
        //3.遍历想读列表，拿到想读的书的条目
        List<MatchBean> matchBeanArrayList = Lists.newArrayList();
        for (int i = 0; i < list.getData().size(); i++) {
            WantRead wantRead = list.getData().get(i);
            Integer id = wantRead.getId();//书籍ID
            MatchBean matchBean = new MatchBean();
            matchBean.setWantRead(wantRead);
            List<HaveRead> haveReads = Lists.newArrayList();
            for (int j = 0; j < allList.getData().size(); j++) {
                HaveRead haveRead = allList.getData().get(j);
                if (haveRead.getId() == id) { //匹配成功，则添加到集合中，我要返回的集合是个树形集合，外面一层是我们想读的数据，里面一层是匹配成功的数据
                    haveReads.add(haveRead);
                }
            }
            matchBean.setHaveReadList(haveReads);
            matchBeanArrayList.add(matchBean);
        }
        return ServerResponse.createBySuccess(matchBeanArrayList);
    }
}
